﻿using LumiSoft.Net.Log;
using Newtonsoft.Json.Linq;
using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections.Concurrent;

namespace SUNRPA.CONTROLS.IMPL.controls {
    /// <summary>
    /// TODO: 1、等待下载完成超时
    /// </summary>
    public class DealDownLoadDialogNAction : BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            DealDownLoadDialogNParam? param = item.ControlParam as DealDownLoadDialogNParam;
            BrowserType browType = param.BrowType;
            int buttonType = param.ButtonType;
            
            //保存父路径
            string saveParentPath = param.SavePath;
            this.GetStringVar(ref saveParentPath, globleData);
            
            bool renameDownloadFile = param.RenameDownloadFile;
            string customName = param.SaveFileName;
            if(!renameDownloadFile){ 
                customName = "";
            }
            this.GetStringVar(ref customName, globleData);

            string savePathOutVarName = param.FileSavePathOutVar;
            this.GetStringVar(ref savePathOutVarName, globleData);

            string waitDialogTime = param.WaitDialogTime;
            this.GetStringVar(ref waitDialogTime, globleData);
            int wdt = int.Parse(waitDialogTime);

            bool waitDownloadFinish = param.WaitingDownloadFinish;
            string downloadExpireTimeString = param.ExpireTime;
            this.GetStringVar(ref downloadExpireTimeString, globleData);
            int downloadExpireTime = int.Parse(downloadExpireTimeString);

            NamePipeRequest request = new NamePipeRequest();
            BrowserUtil browserUtil;
            switch(browType){ 
                case BrowserType.GOOGLE:
                    browserUtil = new ChromeUtil();
                    //处理下载对话框
                    browserUtil.DealDownLoadDialog(buttonType == 0 ? true : false, saveParentPath, customName, wdt);
                    JObject downloadItem = (JObject)browserUtil.SearchLatestDownloadTask();
                    if(buttonType == 0) { 
                        if(waitDownloadFinish) {
                            //保存，查询当前最新添加的下载任务
                            int downloadId = (int)downloadItem["id"];
                            bool isExpire = browserUtil.IsDownloadExpire(downloadId, downloadExpireTime);
                            if(isExpire) { 
                                throw new RpaRuntimeException("下载超时");
                            }
                        }
                        string downLoadFilePath = downloadItem["filename"].ToString();
                        if(downLoadFilePath == null) {
                            throw new RpaRuntimeException("下载出错，返回下载路径为空");
                        }
                        this.saveVar(globleData, savePathOutVarName, new StringModel(savePathOutVarName, downLoadFilePath), DefaultContents.RUNNING_PARAM);
                    }
                    break;
                case BrowserType.IE:
                    break;
                case BrowserType.EDGE:
                    break;
                default:
                    throw new RpaRuntimeException("浏览器类型错误");
            }
            return currentIndex;
        }
    }
}
